package com.tchhy.mvplibrary.ui.adapter

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.PagerAdapter

open class CommonBaseFragmentPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
    private var fragments: MutableList<Fragment>? = null
    private var titles: List<String>? = null

    override fun getCount(): Int {
        return if (fragments == null) 0 else fragments!!.size
    }

    override fun getItem(position: Int): Fragment {
        return fragments!![position]
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return if (titles == null) null else titles!![position]
    }

    fun setFragments(fragments: List<Fragment>) {
        this.fragments = fragments.toMutableList()
    }

    fun setTitles(titles: List<String>) {
        this.titles = titles
    }

    override fun getItemId(position: Int): Long {
        fragments?.let {
            return it[position].hashCode().toLong()
        }
        return super.getItemId(position)
    }

    override fun getItemPosition(`object`: Any): Int {
        return PagerAdapter.POSITION_NONE
    }
}
